#!/usr/bin/env python
#coding:utf-8
"""
  Author:  xulie --<>
  Purpose: 
  Created: 06/29/16
"""
from peewee import Cast
from pymysql import NUMBER

FUNC_DIC = {
    'date': lambda x, y: x == (y if len(y)==10 else int(y)/1000),
    'date_gt': lambda x, y: x > (y if len(y)==10 else int(y)/1000),
    'date_lt': lambda x, y: x < (y if len(y)==10 else int(y)/1000),
    'date_gte': lambda x, y: x >= (y if len(y)==10 else int(y)/1000),
    'date_lte': lambda x, y: x <= (y if len(y)==10 else int(y)/1000),
}

def makeQueryFields(kwargs, Table):
    def _get_field(slug):
        return getattr(Table, slug)

    filters = []
    for k, v in kwargs.items():
        if '__' in k:
            f_name, func_name = k.split('__')
            field = _get_field(f_name)
            if v:
                filters.append(FUNC_DIC[func_name](field, v[0]))
        else:
            if hasattr(Table,k):
                field = _get_field(k)
                filters.append(field == v)
    return filters

def jsonPropt(astr):#给json内的键加上双引号
    return astr.replace(' ','').replace('\n','').replace('\r','')\
               .replace("'",'"')